package com.quanyou.base.framework.dao.mybatis;

import com.quanyou.base.framework.dao.domain.BaseDomain;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.base.delete.DeleteMapper;
import tk.mybatis.mapper.common.base.select.SelectCountMapper;
import tk.mybatis.mapper.common.base.select.SelectMapper;
import tk.mybatis.mapper.common.base.select.SelectOneMapper;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.List;

/**
 * 基础Mapper
 * Created by chenlichao on 15/6/26.
 */
public interface BasicMapper<D extends BaseDomain<PK>, PK extends Serializable> extends
        SelectCountMapper<D>,
        SelectMapper<D>,
        SelectOneMapper<D>,
        //BaseUpdateMapper<D>,
        DeleteMapper<D> {

    @InsertProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int insert(D record);

    @InsertProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int insertSelective(D record);

    /**
     * 批量插入，支持数据库自增字段，支持回写
     *
     * @param recordList 记录列表
     * @return 插入数
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @InsertProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int insertList(List<D> recordList);

    @SelectProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    D getByPrimaryKey(PK id);

    @DeleteProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int deleteByPrimaryKey(PK id);

    @UpdateProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int updateByPrimaryKey(D domain);

    @UpdateProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int updateSelectiveByPrimaryKey(D domain);

    @SelectProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    List<D> listAll();

    @SelectProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    List<D> selectByExample(Example example);

    @SelectProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int selectCountByExample(Example example);

    @DeleteProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int deleteByExample(Example example);

    @UpdateProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int updateByExample(@Param("record") D record, @Param("example") Example example);

    @UpdateProvider(type = BasicMapperProvider.class, method = "dynamicSQL")
    int updateByExampleSelective(@Param("record") D record, @Param("example") Example example);

}
